package com.key.controller.admin.assetManagement;

import com.alibaba.fastjson.JSONObject;
import com.key.common.R;
import com.key.common.dto.AssetChangeAdvDTO;
import com.key.common.dto.AssetFCAdvDTO;
import com.key.common.dto.AssetFCDTO;
import com.key.common.vo.AssetChangeVO;
import com.key.common.vo.AssetFCVO;
import com.key.common.vo.AssetUpdateFCVO;
import com.key.entity.AssetChange;
import com.key.service.AssetFinanceChangeService;
import io.swagger.annotations.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
*
 * @description:财务信息变更的Controller
 * @author: jianjiawei
 * @date: 2023/2/18 9:25
 **/
@Api(value = "财务信息变更API",tags = "财务信息变更API")
@Controller
@RequestMapping("/assetsFC")
public class AssetsFinanceChangeController {

    @Autowired
    AssetFinanceChangeService afcService;
    /*
    *
     * @description:新建财务信息变更的表单数据回显
     * @author: jianjiawei
     * @date: 2023/2/18 9:29
     **/
    @ApiOperation(value = "回显新建财务信息变更的接口",notes = "新建财务信息变更的表单数据回显",httpMethod = "get", response = R.class)
    @RequestMapping(value = "/showAssetsFinanceChangeNew",method = RequestMethod.GET)
    @ResponseBody
    public R showAssetsFinanceChangeNew(){

        return afcService.showAssetsFinanceChangeNew();
    }

    /*
    *
     * @description:财务信息变更保存的接口
     * @author: jianjiawei
     * @date: 2023/2/18 9:52
     **/
    @ApiOperation(value = "财务信息变更保存的接口",notes = "创建新的财务信息变更清单",httpMethod = "post", response = R.class)
    @ResponseBody
    @RequestMapping(value = "/saveAssetsFinanceChange",method = RequestMethod.POST)
    public R saveAssetsFinanceChange(@RequestBody @ApiParam(name = "AssetFCDTO",value = "财务信息变更数据传输对象",required = true) AssetFCDTO fcDTO){
        System.out.println(fcDTO);
        return afcService.saveAssetsFinanceChange(fcDTO);
    }

    /*
    *
     * @description:查询财务信息变更列表的接口
     * @author: jianjiawei
     * @date: 2023/2/18 10:24
     **/
    @ApiOperation(value = "查询财务信息变更列表",notes = "显示财务信息变更列表信息",httpMethod = "post", response = AssetFCVO.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage",value = "当前页",paramType = "query",dataType = "int",required = true),
            @ApiImplicitParam(name = "pageSize",value = "每页显示总数",paramType = "query",dataType = "int",required = true)
    })
    @ResponseBody
    @RequestMapping(value = "/showAssetsFinanceChangeList",method = RequestMethod.GET)
    public R showAssetsFinanceChangeList(@RequestParam(name = "currentPage") Integer currentPage,@RequestParam(name = "pageSize") Integer pageSize){

        return afcService.showAssetsFinanceChangeList(currentPage, pageSize);
    }

    /*
    *
     * @description:回显修改财务信息变更的接口
     * @author: jianjiawei
     * @date: 2023/2/18 10:45
     **/
    @ApiOperation(value = "回显修改财务信息变更的接口",notes = "修改财务信息变更的表单数据回显",httpMethod = "get", response = AssetUpdateFCVO.class)
    @ApiImplicitParam(name = "id",value = "主键ID",paramType = "query",dataType = "int",required = true)
    @RequestMapping(value = "/showAssetsFinanceChangeUpdate",method = RequestMethod.GET)
    @ResponseBody
    public R showAssetsFinanceChangeUpdate(@RequestParam(name = "id") Integer id){

        return afcService.showAssetsFinanceChangeUpdate(id);
    }

    /*
    *
     * @description:财务信息变更保存的接口
     * @author: jianjiawei
     * @date: 2023/2/18 11:09
     **/
    @ApiOperation(value = "修改财务信息变更保存的接口",notes = "修改财务信息变更清单",httpMethod = "post", response = R.class)
    @ResponseBody
    @RequestMapping(value = "/updateAssetsFinanceChange",method = RequestMethod.POST)
    public R updateAssetsFinanceChange(@RequestBody @ApiParam(name = "AssetFCDTO",value = "财务信息变更数据传输对象",required = true) AssetFCDTO fcDTO){

        return afcService.updateAssetsFinanceChange(fcDTO);
    }

    @ApiOperation(value = "删除财务信息变更的数据",notes = "删除财务信息数据",httpMethod = "post", response = R.class)
    @ApiImplicitParam(name = "id",value = "主键ID",paramType = "query",dataType = "int",required = true)
    @RequestMapping(value = "/deleteAssetsFinanceChange",method = RequestMethod.POST)
    @ResponseBody
    public R deleteAssetsFinanceChange(@RequestParam(name = "id") Integer id){

        return afcService.deleteAssetsFinanceChange(id);
    }

    @ApiOperation(value = "根据高级搜索查询财务信息变更接口",notes = "根据高级搜索查询所有财务信息变更列表",httpMethod = "post", response = AssetFCVO.class)
    @ResponseBody
    @RequestMapping(value = "/findFCListByAdv",method = RequestMethod.POST)
    public R findFCListByAdv(@RequestBody @ApiParam(name = "AssetFCDTO",value = "财务信息变更高级搜索条件",required = true) AssetFCAdvDTO advDTO){

        return afcService.findFCListByAdv(advDTO);
    }


    @RequestMapping(value = "/exportExcel",method = RequestMethod.GET)
    @ApiOperation(value = "EXCEL导出",notes = "把财务变更信息导出为excel",httpMethod = "get")
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=401,message ="用户未授权"),
            @ApiResponse(code=403,message="认证失败"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public void exportExcel(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HSSFWorkbook workbook = afcService.getWorkbook();
        String userAgent = request.getHeader("User-Agent");
        //excel文件名
        String fileName = "财务信息变更表"+System.currentTimeMillis()+".xls";
        // 针对IE或者以IE为内核的浏览器：
        if (userAgent.contains("MSIE") || userAgent.contains("Trident")) {
            fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
        } else {
            // 非IE浏览器的处理：
            fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
        }
        //响应到客户端
        try {
            this.setResponseHeader(response, fileName);
            OutputStream os = response.getOutputStream();
            response.setContentType("application/x-xls;charset=utf-8");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-Type", MediaType.APPLICATION_OCTET_STREAM_VALUE);
            response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
            workbook.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //发送响应流方法
    public void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            fileName = new String(fileName.getBytes(), "ISO8859-1");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }





}
